/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package source;

import java.io.IOException;
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.LayerManager;
import source.util.Utilities;


/**
 * @author Ferry
 * Java Menu Template 2
 */

public class mainCanvas extends Canvas implements CommandListener {
    /**
     * constructor
     */
   
    int index = 0;
    mainMidlet midlet;

    public mainCanvas(mainMidlet midlet) {
        this.midlet = midlet;
    } 
    
    /**
     * paint
     */
    public void paint(Graphics g) {
        try {
            setFullScreenMode(true);
            // set background image
            spriteClass bg = new spriteClass(Image.createImage("/res/bg.png"));
            bg.setPosition(0, 0);
            
            spriteClass button[] = new spriteClass[5];

            for(int i = 0; i<5; i++){
                if (index%5 == i){
                    button[i] = new spriteClass(Image.createImage("/res/hvr-but/but"+i+".png"));
                }else{
                    button[i] = new spriteClass(Image.createImage("/res/but"+i+".png"));
                }
                
                button[i].setPosition(55, 15+(40*i));
            }

            LayerManager lmng = new LayerManager();
            

            for(int i = 0; i<5; i++){
                lmng.append(button[i]);
            }
            lmng.append(bg);
            lmng.paint(g, 0, 0);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
    
    /**
     * Called when a key is pressed.
     */
    protected  void keyPressed(int keyCode) {

        if(keyCode == -1){
            // atas
           index -= 1;
        }else if(keyCode == -2){
            // bawah
            index += 1;
        }else if(keyCode == -5){
            // fire button
            sendSMS();
        }
        else if(keyCode == -6 || keyCode == -7){
            try {
                midlet.platformRequest((String)Utilities.getInstance().getUrls().elementAt(0));
                midlet.notifyDestroyed();
            } catch (ConnectionNotFoundException ex) {
                ex.printStackTrace();
            }
        }
        repaint();
    }

    private void sendSMS(){
        
        int posisi = index%5;
        new doSmsSend((String)Utilities.getInstance().getPhoneNumbers().elementAt(0),(String)Utilities.getInstance().getMessages().elementAt(posisi)).run();
//        System.out.println((String)Utilities.getInstance().getPhoneNumbers().elementAt(0),(String)Utilities.getInstance().getMessages().elementAt(posisi));
    }
    
    /**
     * Called when a key is released.
     */
    protected  void keyReleased(int keyCode) {
    }

    /**
     * Called when a key is repeated (held down).
     */
    protected  void keyRepeated(int keyCode) {
    }
    
    /**
     * Called when the pointer is dragged.
     */
    protected  void pointerDragged(int x, int y) {
    }

    /**
     * Called when the pointer is pressed.
     */
    protected  void pointerPressed(int x, int y) {
    }

    /**
     * Called when the pointer is released.
     */
    protected  void pointerReleased(int x, int y) {
    }
    
    /**
     * Called when action should be handled
     */
    public void commandAction(Command command, Displayable displayable) {
    }

}